package com.istudy.servlet;

import com.istudy.entity.User;
import com.istudy.entity.UserType;
import com.istudy.service.interfaces.UserService;
import com.istudy.service.imp.UserServiceImp;
import com.istudy.utils.JsonMessage;
import com.istudy.utils.ReturnResult;
import pers.dzk.jdk.entity.PageBean;
import pers.dzk.jdk.selevt.Transverter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends AbstractServlet {

    private static UserService us = new UserServiceImp();

    @Override
    public Class getServletClass() {
        return UserServlet.class;
    }

    public JsonMessage UserDeduction(HttpServletRequest request, HttpServletResponse response) {
        String userId = request.getParameter("userId");
        String videoConsume = request.getParameter("videoConsume");
        JsonMessage jsonMessage=new JsonMessage();//json消息类
        boolean b = us.UserDeduction(userId, videoConsume);
        jsonMessage.setDeduction(b);
        return jsonMessage;
    }

    public ReturnResult register(HttpServletRequest req, HttpServletResponse resp) {
            User user = Transverter.toEntity(User.class, req);
            ReturnResult rr = new ReturnResult();
            boolean register = us.register(user);
            if(register){
                rr.setStatus(1);
                rr.setMessage("注册成功！");
            }else {
                rr.setStatus(-1);
                rr.setMessage("注册失败！");
            }
            return rr;
    }

    public ReturnResult login(HttpServletRequest req, HttpServletResponse resp) {
        ReturnResult returnResult = new ReturnResult();
            User user1 = Transverter.toEntity(User.class, req);
            User user2 = us.login(user1);
            if(user2 == null){
                returnResult.setStatus(-1);
                returnResult.setMessage("账号或密码错误！");
            }else {
                returnResult.setStatus(1);
                returnResult.setMessage("登入成功");
                HttpSession session = req.getSession();
                session.setAttribute("user", user2);
            }
        return returnResult;
    }

    public ReturnResult repetitionLoginName(HttpServletRequest req, HttpServletResponse resp){
        ReturnResult returnResult = new ReturnResult();
            User user1 = Transverter.toEntity(User.class, req);
            boolean repetition = us.repetitionLoginName(user1);
            if(repetition){
                returnResult.setStatus(-1);
                returnResult.setMessage("该登陆名重复！");
            }else {
                returnResult.setStatus(1);
                returnResult.setMessage("该登陆名可用！");
            }
        return returnResult;
    }

    public String exitLogin(HttpServletRequest req, HttpServletResponse resp){
        req.getSession().removeAttribute("user");
        VideoServlet videoServlet = new VideoServlet();
        return videoServlet.getshowlist(req,resp);
    }


     public String personInfo(HttpServletRequest req, HttpServletResponse resp) {
            if(req.getSession().getAttribute("user")!=null){
            return "homePage";
            }else {
                return "login";
            }
    }

    public String userManage(HttpServletRequest req, HttpServletResponse resp){
        int pageNo;
        try {
            pageNo = Integer.parseInt(req.getParameter("pageNo"));
        }catch (Exception e){
            pageNo = 1;
        }
        PageBean<User> pb = us.userManage(pageNo,5);
        req.setAttribute("data0",pb);
        return "userManage";
    }

    public String userToUpdate(HttpServletRequest req, HttpServletResponse resp){
        Integer userId;
        try {
            userId = Integer.parseInt(req.getParameter("userId"));
        }catch (Exception e){
            userId = 1;
            e.printStackTrace();
        }
        User user = new User();
        user.setUserId(userId);
        User user2 = us.userToUpdate(user);
        List<UserType> userType = us.selectUserType();
        req.setAttribute("data0",user2);
        req.setAttribute("data1",userType);
        return "userUpdate";
    }

    public String deleteUser(HttpServletRequest req, HttpServletResponse resp){
        User user = Transverter.toEntity(User.class,req);
        us.deleteUser(user);
        return userManage(req,resp);
    }

    public ReturnResult userUpdate(HttpServletRequest req, HttpServletResponse resp){
        User user = Transverter.toEntity(User.class,req);
//        String userName = null;
        ReturnResult rr = new ReturnResult();
//        try {
//            userName = new String(req.getParameter("userName").getBytes("iso-8859-1"), "utf-8");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        if(us.userUpdate(user)){
            rr.setStatus(1);
            rr.setMessage("修改成功！");
        }else {
            rr.setStatus(-1);
            rr.setMessage("修改失败！");
        }
        return rr;
    }

}
